// 修改后的 utils/markdown.ts
import hljs from 'highlight.js'

// 修改后的高亮函数
export const setupHighlight: MarkdownIt.HighlightFunction = (code, lang) => {
  const language = hljs.getLanguage(lang) ? lang : 'plaintext';
  
  try {
    const highlighted = hljs.highlight(code, { 
      language, 
      ignoreIllegals: true 
    }).value;

    const lines = highlighted.split('\n');
    if (lines.length > 0 && lines[lines.length - 1] === '') {
      lines.pop();
    }

    // ▼▼▼ 关键结构修改 ▼▼▼
    return `<div class="code-grid">
          ${lines.map((line, i) => `
            <div class="line-group" data-line="${i + 1}">
              <div class="line-number"></div>
              <div class="code-line">${line || ' '}</div>
            </div>
          `).join('')}
        </div>`;
    // ▲▲▲ 结构修改结束 ▲▲▲
  } catch (e) {
    return `<pre><code>${hljs.highlight('plaintext', code).value}</code></pre>`;
  }
}